#include "controller.h"

void Controller::parseSVG(QByteArray image)
{
    doc.setContent(image);
    allShapes << doc.elementsByTagName("rect");
    allShapes << doc.elementsByTagName("circle");
    allShapes << doc.elementsByTagName("ellipse");
    allShapes << doc.elementsByTagName("line");
    allShapes << doc.elementsByTagName("polyline");
    allShapes << doc.elementsByTagName("polygon");
    allShapes << doc.elementsByTagName("path");
}

QVector<QStringList> Controller::getAllShapes()
{
    QVector<QStringList> rows;
    for(uint i=0; i<allShapes.length(); i++)
    {
        QStringList l;
        l<<allShapes.at(i).nodeName();
        QString name = allShapes.at(i).toElement().attribute("name", "");
        l<<name;
        QString description = allShapes.at(i).toElement().attribute("description", "");
        l<<description;
        QString categories = allShapes.at(i).toElement().attribute("categories", "");
        l<<categories;
        QString adresses = allShapes.at(i).toElement().attribute("adresses", "");
        l<<adresses;
        QString phone_numbers = allShapes.at(i).toElement().attribute("phone_numbers", "");
        l<<phone_numbers;
        QString email_adresses = allShapes.at(i).toElement().attribute("email_adresses", "");
        l<<email_adresses;
        QString sites = allShapes.at(i).toElement().attribute("sites", "");
        l<<sites;
        rows<<l;
    }
    return rows;
}

void Controller::run()
{
    emit sendShapes(getAllShapes());
    exec();
}

void Controller::onShapeSelected(uint index)
{
    QByteArray imageData = doc.toByteArray();
    QByteArray selectionLayer("<g");
    selectionLayer+=common::end+(QByteArray)"label=\"selected layer\""+common::end+(QByteArray)"groupmode=\"layer\""+common::end+(QByteArray)"id=\"selected layer\">";
    selectionLayer+=common::end+(QByteArray)"<"+allShapes.at(index).nodeName()+common::end;
    for(uint i=0;i<allShapes.at(index).attributes().size();i++)
    {
        if ( ( allShapes.at(index).attributes().item(i).nodeName()!="id" ) &&
             (allShapes.at(index).attributes().item(i).nodeName()!="style")  &&
             (allShapes.at(index).attributes().item(i).nodeName()!="fill"))
        {
            selectionLayer+=allShapes.at(index).attributes().item(i).nodeName()+(QByteArray)"=\""+allShapes.at(index).attributes().item(i).nodeValue()+(QByteArray)"\""+common::end;
        }

        if(allShapes.at(index).attributes().item(i).nodeName()=="id")
        {
            selectionLayer+=allShapes.at(index).attributes().item(i).nodeName()+(QByteArray)"=\"selected_figure\""+common::end;
        }
    }
    selectionLayer+=(QByteArray)"style=\"fill:#00ffff;fill-opacity:0.69411767\""+common::end;
    selectionLayer+=(QByteArray)"/>"+common::end;
    selectionLayer+=(QByteArray)"</g>"+common::end;
    imageData.insert(imageData.indexOf("</svg>"),selectionLayer);
    emit redrawImage(imageData);
}

void Controller::onOpenFile(QString fileName)
{
    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QByteArray image = file.readAll();
    parseSVG(image);
    emit redrawImage(image);
    emit sendShapes(getAllShapes());
    file.close();
}

QByteArray Controller::getResultSVG(QVector<QStringList> cells)
{
    QByteArray result;
    for(uint i=0; i<allShapes.length(); i++)
    {
//        header<<"type"<<"name"<<"description"<<"categories"<<"adresses"<<"phone numbers"<<"email adresses"<<"sites";
        if(2 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("name", cells.at(i).at(1));
        }
        if(3 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("description", cells.at(i).at(2));
        }
        if(4 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("categories", cells.at(i).at(3));
        }
        if(5 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("adresses", cells.at(i).at(4));
        }
        if(6 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("phone_numbers", cells.at(i).at(5));
        }
        if(7 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("email_adresses", cells.at(i).at(6));
        }
        if(8 <= cells.at(i).length())
        {
            allShapes.at(i).toElement().setAttribute("sites", cells.at(i).at(7));
        }
    }
    result = doc.toByteArray();
    return result;
}

void Controller::onSaveFile(QString fileName, QVector<QStringList> cells)
{
    QString nameOfFile = fileName;
    if(!nameOfFile.endsWith(".svg"))
    {
        nameOfFile+=".svg";
    }
    QFile file(nameOfFile);
    if(!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::critical(NULL, "File processing error", "Couldn't open file "+nameOfFile, QMessageBox::Ok);
    }
    file.write(getResultSVG(cells));
    file.flush();
    file.close();
}

void Controller::onCloseFile(void)
{
    doc.setContent(QString(""));
    allShapes.clear();
}
